package com.manager.container.controller;

import com.manager.common.customer.utils.RemoteExecute;
import com.manager.common.utils.DataTools;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.container.service.NetworkCardService;
import com.manager.model.NetworkCard;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/networkCard")
public class NetworkCardController {

    @Autowired
    NetworkCardService networkCardService;

    @RequestMapping("/getNetworkCardPage")
    public PageVO getNetworkCardPage(NetworkCard model) {
        return networkCardService.getNetworkCardPage(model);
    }

    @RequestMapping("/addOrUpdate")
    public ResultVO addOrUpdate(NetworkCard model) {
        try {
            if(StringUtils.isNotEmpty(model.getId())){
                networkCardService.updateNetworkCard(model);
            }else {
                networkCardService.addNetworkCard(model);
            }
            return ResultVO.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/delete")
    public ResultVO delete(NetworkCard model) {
        try {
            NetworkCard networkCard = networkCardService.getNetworkCardById(model);
            if(1 == networkCard.getNetworkStatus()){
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("docker network rm ").append(networkCard.getName());
                ResultVO<Object[]> resultVO = RemoteExecute.executeSsh(stringBuffer.toString());
                if(resultVO.getCode() != 1){
                    return ResultVO.success("删除远程docker网络失败!");
                }
            }
            networkCardService.deleteNetworkCard(model);
            return ResultVO.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/updateStatus")
    public ResultVO updateStatus(NetworkCard model) {
        try {
            networkCardService.updateNetworkCardStatus(model);
            return ResultVO.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/createNetwork")
    public ResultVO createNetwork(NetworkCard model) {
        try{
            NetworkCard networkCard = networkCardService.getNetworkCardById(model);
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("docker network create  ")
                    .append(" --driver ").append(DataTools.getDicTextByGroupIdAndVal("docker_network_type",networkCard.getNetworkType()+"")).append(" ")
                    .append(" --subnet=").append(networkCard.getSubnet()).append(" ")
                    .append(" --gateway=").append(networkCard.getGateway()).append(" ")
                    .append(networkCard.getName());
            ResultVO<Object[]> resultVO = RemoteExecute.executeSsh(stringBuffer.toString());
            if(resultVO.getCode() != 1){
                StringBuffer result = new StringBuffer();
                for(Object data : resultVO.getData()){
                    result.append(data.toString()).append(";");
                }
                networkCard.setNetworkStatus(-1);
                networkCardService.updateNetworkCardStatus(networkCard);
                return ResultVO.failed("生成docker网络失败!" + "【"+result+"】");
            }
            networkCard.setNetworkStatus(1);
            networkCardService.updateNetworkCardStatus(networkCard);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/getNetworkCardList")
    public ResultVO getNetworkCardList(NetworkCard model) {
        try{
            List<NetworkCard> list = networkCardService.getNetworkCardList(model);
            return ResultVO.success(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }
}
